package org.example.springboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.example.springboot.common.Result;
import org.example.springboot.entity.Comment;
import org.example.springboot.service.CommentService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 评论控制器
 */
@Tag(name="评论管理接口")
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentService commentService;
    
    @Operation(summary = "添加评论")
    @PostMapping("/add")
    public Result<?> addComment(@RequestBody Map<String, Object> params) {
        Long bookId = Long.valueOf(params.get("bookId").toString());
        String content = (String) params.get("content");
        Long parentId = params.get("parentId") != null ? Long.valueOf(params.get("parentId").toString()) : null;
        
        if (bookId == null) {
            return Result.error("图书ID不能为空");
        }
        
        if (content == null || content.trim().isEmpty()) {
            return Result.error("评论内容不能为空");
        }
        
        Long commentId = commentService.addComment(bookId, content, parentId);
        return Result.success("评论成功", commentId);
    }
    
    @Operation(summary = "删除评论")
    @PostMapping("/delete")
    public Result<?> deleteComment(@RequestBody Map<String, Long> params) {
        Long commentId = params.get("commentId");
        if (commentId == null) {
            return Result.error("评论ID不能为空");
        }
        
        commentService.deleteComment(commentId);
        return Result.success("删除成功");
    }
    
    @Operation(summary = "管理员删除评论")
    @PostMapping("/admin/delete")
    public Result<?> adminDeleteComment(@RequestBody Map<String, Long> params) {
        Long commentId = params.get("commentId");
        if (commentId == null) {
            return Result.error("评论ID不能为空");
        }
        
        commentService.adminDeleteComment(commentId);
        return Result.success("删除成功");
    }
    
    @Operation(summary = "获取图书评论列表")
    @GetMapping("/book")
    public Result<?> getBookComments(
            @RequestParam Long bookId,
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Comment> page = commentService.getBookComments(bookId, currentPage, size);
        return Result.success(page);
    }
    
    @Operation(summary = "获取用户的评论列表")
    @GetMapping("/user")
    public Result<?> getUserComments(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Comment> page = commentService.getUserComments(currentPage, size);
        return Result.success(page);
    }
    
    @Operation(summary = "管理员获取所有评论")
    @GetMapping("/admin/list")
    public Result<?> getAllComments(
            @RequestParam(required = false) Long bookId,
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Comment> page = commentService.getAllComments(bookId, currentPage, size);
        return Result.success(page);
    }
} 